package com.ptbuy.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.ptbuy.R;
import com.ptbuy.utils.MineType;
import com.ptbuy.utils.ToastUtil;
import com.ptbuy.video.CustomVideoView;

import java.io.File;
import java.util.List;

public class MediaPagerAdapter extends PagerAdapter {

    private List<String> imageUrls;
    private LayoutInflater inflater;
    private Context mContext;
    private int type;

    public interface OnItemDeleteListener {
        void DeleteItem(String filePath, int type);
    }

    private OnItemDeleteListener listener;

    public void setOnItemDeleteListener(OnItemDeleteListener listener) {
        this.listener = listener;
    }


    public MediaPagerAdapter(Context context, List<String> imageUrls, int type) {
        this.imageUrls = imageUrls;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.type = type;
    }

    @Override
    public int getCount() {
        return imageUrls.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        String s = imageUrls.get(position);
        File file = new File(s);
        View view;
        if (s.contains(".mp4")) {
            view = inflater.inflate(R.layout.activity_item_media_video, container, false);
            CustomVideoView videoView = (CustomVideoView) view.findViewById(R.id.videoView);
            ImageButton videoControl = (ImageButton) view.findViewById(R.id.videoControl);
            videoControl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (file.exists()) {
                        MineType.openFile(mContext, file);
                    } else {
                        ToastUtil.show(mContext, "照片丢失，请长安删除后重新拍摄");
                    }
                }
            });

            videoView.setOnLongClickListener(v -> {
                AlertDialog dialog = new AlertDialog.Builder(mContext)
                        .setTitle("系统提示")
                        .setMessage("删除后不能恢复！是否删除？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                listener.DeleteItem(s, type);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
                return true;
            });


        } else {
            view = inflater.inflate(R.layout.activity_item_media, container, false);
            ImageView imageView = view.findViewById(R.id.imageView);

            imageView.setOnClickListener(v -> {
                if (file.exists()) {
                    MineType.openFile(mContext, file);
                } else {
                    ToastUtil.show(mContext, "照片丢失，请长安删除后重新拍摄");
                }
            });
            imageView.setOnLongClickListener(v -> {
                AlertDialog dialog = new AlertDialog.Builder(mContext)
                        .setTitle("系统提示")
                        .setMessage("删除后不能恢复！是否删除？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                listener.DeleteItem(s, type);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
                return true;
            });
            // 使用图片加载库（如 Glide 或 Picasso）来加载图片
            Glide.with(container.getContext())
                    .load(imageUrls.get(position))
                    .into(imageView);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}

